import { computed } from 'vue'
import { useStore } from 'vuex'

// 获取商品列表
export const useCommonCartEffect = (shopId) => {
  const store = useStore()
  const cartList = store.state.cartList
  const productList = computed(() => {
    const product = cartList[shopId]?.productListObject || {}
    const notEmptyProductList = {}
    for (const i in product) {
      if (product[i].count > 0) {
        notEmptyProductList[i] = product[i]
      }
    }
    return notEmptyProductList
  })
  return { productList }
}

export const useCommonAllListCartEffect = () => {
  const store = useStore()
  const cartList = store.state.cartList
  let productList = {}
  const productListAllCount = computed(() => {
    if (cartList) {
      for (const i in cartList) {
        productList = cartList[i].productListObject
        const notEmptyProductList = {}
        for (const j in productList) {
          if (productList[j].count > 0) {
            notEmptyProductList[j] = productList[j]
          }
        }
        cartList[i].productListObject = notEmptyProductList
      }
    }
    return cartList
  })
  return { productListAllCount }
}
